#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/dir.h>
#include <sys/param.h>
#include "nPatriciaTrie.h"
#include "linkedlist.h"
#include "sharedseg.h"
#include "client.h"
#include "fileperm.h"
#include "getabsmod.h"

#define NOIPCNOTIFICATION
#define VERIFYPERMISSION

key_t key;
linkedlist* tmplist;

/*! Check for signals (like SIGINT, SIGQUIT, SIGTERM ..), if found exits the program */
void clearsignals() {
        if (signal(SIGINT,SIG_DFL)==SIG_ERR) {
		perror("signal");
		exit(1);
	}
        if (signal(SIGQUIT,SIG_DFL)==SIG_ERR) {
		perror("signal");
		exit(1);
	}
        if (signal(SIGTERM,SIG_DFL)==SIG_ERR) {
		perror("signal");
		exit(1);
	}
}

/*! Opens the connection with server
 *      - Sets up the 3 semaphores
 *      - Attaches to the shared memory used by server
 *      - Gets the pointer for shared memory segment
 */
void inithandshake() {

	key = ftok(TEXT,CHAR);

	clearsignals();

	/* Set up the 3 semaphores */
        semid = semget(key, 3, 0666);
        if (semid < 0) {
                //printf("Error - in semaphore %d\n",key);
		printf("Unable to contact local server. Is server running ?\n");
                exit(1);
        }
	#ifndef NOIPCNOTIFICATION
	printf("Semaphore ok!!!\n");
	#endif

        /* Connect to shared memory */
	if((shmid = shmget(key, SEGSIZE, 0666)) == -1) 
        {
                printf("Shared memory segment not found!! is server instance running ??\n");
		exit(1);
        }
        
	#ifndef NOIPCNOTIFICATION
	printf("connected to Shared memory segment!!!\n");
	#endif

        /* Attach (map) the shared memory segment into the current process */
        if((segptr = (struct shared*) shmat(shmid, 0, 0)) == NULL)
        {
                perror("shmat");
                exit(1);
        }
        
	#ifndef NOIPCNOTIFICATION
	printf("Memory segment associated with data!!!\n");
	#endif
	

	// Wait for other clients
	#ifndef NOIPCNOTIFICATION
	printf("Waiting for other clients to complete!!!\n");
	#endif
       
       	sem_wait(anyclient);
	
	#ifndef NOIPCNOTIFICATION
	printf("Ok i can work now!!!\n");
	#endif
}

/*! Closes the connection with server
 *      - "NONE" is written to the result part
 *      - client_sem & anyclient are is signalled
 *      - All the signals left out are cleared
 */
void closehandshake() {
	//printf("\nAcknowledge : %s \n",segptr->result);
	strcpy(segptr->result,"NONE");
	#ifndef NOIPCNOTIFICATION
	printf("Reset client signal!!!\n");
	#endif
	sem_signal(client_sem);	
	#ifndef NOIPCNOTIFICATION
	printf("Ok client is done notify anyclient!!!\n");
	#endif
	sem_signal(anyclient);
	if ((shmdt(segptr))==-1) {
		printf("Error in shmdt()");
		exit(-1);
	}
	clearsignals();
}
void sigint_handler(int sig){
	printf("Client Ends abruptly!");
	segptr->abort=true;
	sem_signal(server_sem);
}


/*!
 * Client API to add tags for a given file
 *    - Initiates connection using inithandshake()
 *    - Waits to get a semaphore 
 *    - Loops over the list of tags
 *    - Adds each tag to the file name
 *    - Closes the connection using closehandshake()
 */
void clientaddtags(char *filename, linkedlist *tags)
{
	if (tags->isEmpty())
		return;
#ifdef VERIFYPERMISSION	
	if (!WRITEACCESS(filename))
		return;
#endif
	tmplist=tags->copyList();
	inithandshake();

	// Wait for Semaphore
	#ifndef NOIPCNOTIFICATION
	printf("Waiting for semaphore(Server to become free)!!!\n");
	#endif
       
       	sem_wait(client_sem);

	// Got Semaphore 
	#ifndef NOIPCNOTIFICATION
	printf("Server is free, sending data!!!\n");
	#endif

	while (!tmplist->isEmpty()) {
		strcpy(segptr->command,"add");
		strcpy(segptr->param1,filename);
		strcpy(segptr->param2,tmplist->remove());
		printf("%s\t%s\t%s\n",segptr->command,segptr->param1,segptr->param2);
		// Release Semaphore
		sem_signal(server_sem);
	//	printf("Waiting for result\n");
		sem_wait(client_sem);	
	//	printf("Result obtained\n");
	}
	closehandshake();
	delete tmplist;
}

void clientremovetags(char *filename, linkedlist *tags)
{
	if (tags->isEmpty())
		return;
#ifdef VERIFYPERMISSION	
	if (!WRITEACCESS(filename))
		return;
#endif
	tmplist=tags->copyList();
	inithandshake();

	// Wait for Semaphore
	#ifndef NOIPCNOTIFICATION
	printf("Waiting for semaphore(Server to become free)!!!\n");
	#endif
       
       	sem_wait(client_sem);

	// Got Semaphore 
	#ifndef NOIPCNOTIFICATION
	printf("Server is free, sending data!!!\n");
	#endif

	while (!tmplist->isEmpty()) {
		strcpy(segptr->command,"remove");
		strcpy(segptr->param1,filename);
		strcpy(segptr->param2,tmplist->remove());
		// Release Semaphore
		sem_signal(server_sem);
		sem_wait(client_sem);	

	}
	closehandshake();
	delete tmplist;
}

linkedlist* clientlisttags(char *filename)
{
#ifdef VERIFYPERMISSION	
	if (!READACCESS(filename))
		return NULL;
#endif
	inithandshake();
	// Wait for Semaphore
	#ifndef NOIPCNOTIFICATION
	printf("Waiting for semaphore(Server to become free)!!!\n");
	#endif
       
       	sem_wait(client_sem);

	// Got Semaphore 
	#ifndef NOIPCNOTIFICATION
	printf("Server is free, sending data!!!\n");
	#endif
       	
       	strcpy(segptr->command,"listtags");
       	strcpy(segptr->param1,filename);
		
	#ifndef NOIPCNOTIFICATION
	printf("Sending command listtags!!!\n");
	#endif
       	
	// Release Semaphore
	sem_signal(server_sem);
	sem_wait(client_sem);
	
	linkedlist* result=new linkedlist();
	int i;	
	if (strcmp(segptr->result,"OVER")!=0) {
		// We have some data
		do {
			for (i=0;i<DTP;i++) {
				if (strlen(segptr->data[i])!=0) {
					result->add(segptr->data[i]);
	//				printf("%s ",segptr->data[i]);
				} else
					break;
			}
			// Handle to server and wait
			// Release Semaphore
			sem_signal(server_sem);
			sem_wait(client_sem);	
		} while ((segptr->moredata)&&(strcmp(segptr->result,"OVER")!=0));
	}
	closehandshake();
	return result;
}

linkedlist* clientlistfiles(char *tag)
{
	inithandshake();
	// Wait for Semaphore
	#ifndef NOIPCNOTIFICATION
	printf("Waiting for semaphore(Server to become free)!!!\n");
	#endif
       
       	sem_wait(client_sem);

	// Got Semaphore 
	#ifndef NOIPCNOTIFICATION
	printf("Server is free, sending data!!!\n");
	#endif
       	
       	strcpy(segptr->command,"listfiles");
       	strcpy(segptr->param1,tag);
		
	#ifndef NOIPCNOTIFICATION
	printf("Sending command listfiles!!!\n");
	#endif
       	
	// Release Semaphore
	sem_signal(server_sem);
	sem_wait(client_sem);
	
	linkedlist* result=new linkedlist();
	int i;	
	if (strcmp(segptr->result,"OVER")!=0) {
		// We have some data
		do {
			for (i=0;i<DTP;i++) {
				if (strlen(segptr->data[i])!=0) {
					result->add(segptr->data[i]);
	//				printf("%s ",segptr->data[i]);
				} else
					break;
			}
			// Handle to server and wait
			// Release Semaphore
			sem_signal(server_sem);
			sem_wait(client_sem);	
		} while ((segptr->moredata)&&(strcmp(segptr->result,"OVER")!=0));
	}
	closehandshake();
#ifdef VERIFYPERMISSION	
	linkedlist *tmp=new linkedlist();
	while ((result!=NULL) && (!result->isEmpty())) {
			char str[300];
			strcpy(str,result->remove());
			if (SOMEACCESS(str))
				tmp->add(str);
	}
	delete result;
	return tmp;
#else
	return result;
#endif
}

linkedlist* clientlistallfiles()
{
	inithandshake();
	// Wait for Semaphore
	#ifndef NOIPCNOTIFICATION
	printf("Waiting for semaphore(Server to become free)!!!\n");
	#endif
       
       	sem_wait(client_sem);

	// Got Semaphore 
	#ifndef NOIPCNOTIFICATION
	printf("Server is free, sending data!!!\n");
	#endif
       	
       	strcpy(segptr->command,"listallfiles");
		
	#ifndef NOIPCNOTIFICATION
	printf("Sending command listallfiles!!!\n");
	#endif
       	
	// Release Semaphore
	sem_signal(server_sem);
	sem_wait(client_sem);
	
	linkedlist* result=new linkedlist();
	int i;	
	if (strcmp(segptr->result,"OVER")!=0) {
		// We have some data
		do {
			for (i=0;i<DTP;i++) {
				if (strlen(segptr->data[i])!=0) {
					result->add(segptr->data[i]);
	//				printf("%s ",segptr->data[i]);
				} else
					break;
			}
			// Handle to server and wait
			// Release Semaphore
			sem_signal(server_sem);
			sem_wait(client_sem);	
		} while ((segptr->moredata)&&(strcmp(segptr->result,"OVER")!=0));
	}
	closehandshake();
	return result;
}

linkedlist* clientgetslashlist()
{
	inithandshake();
	// Wait for Semaphore
	#ifndef NOIPCNOTIFICATION
	printf("Waiting for semaphore(Server to become free)!!!\n");
	#endif
       
       	sem_wait(client_sem);

	// Got Semaphore 
	#ifndef NOIPCNOTIFICATION
	printf("Server is free, sending data!!!\n");
	#endif
       	
       	strcpy(segptr->command,"getslashlist");
		
	#ifndef NOIPCNOTIFICATION
	printf("Sending command listallfiles!!!\n");
	#endif
       	
	// Release Semaphore
	sem_signal(server_sem);
	sem_wait(client_sem);
	
	linkedlist* result=new linkedlist();
	int i;	
	if (strcmp(segptr->result,"OVER")!=0) {
		// We have some data
		do {
			for (i=0;i<DTP;i++) {
				if (strlen(segptr->data[i])!=0) {
					result->add(segptr->data[i]);
			//		printf("%s ",segptr->data[i]);
				} else
					break;
			}
			// Handle to server and wait
			// Release Semaphore
			sem_signal(server_sem);
			sem_wait(client_sem);	
		} while ((segptr->moredata)&&(strcmp(segptr->result,"OVER")!=0));
	}
	closehandshake();
	return result;
}

linkedlist* clientrandomfile()
{
	inithandshake();
	// Wait for Semaphore
	#ifndef NOIPCNOTIFICATION
	printf("Waiting for semaphore(Server to become free)!!!\n");
	#endif
       
       	sem_wait(client_sem);

	// Got Semaphore 
	#ifndef NOIPCNOTIFICATION
	printf("Server is free, sending data!!!\n");
	#endif
       	
       	strcpy(segptr->command,"randomfile");
		
	#ifndef NOIPCNOTIFICATION
	printf("Sending command randomfile!!!\n");
	#endif
       	
	// Release Semaphore
	sem_signal(server_sem);
	sem_wait(client_sem);
	
	linkedlist* result=new linkedlist();
	int i;	
	if (strcmp(segptr->result,"OVER")!=0) {
		// We have some data
		do {
			for (i=0;i<DTP;i++) {
				if (strlen(segptr->data[i])!=0) {
					result->add(segptr->data[i]);
			//		printf("%s ",segptr->data[i]);
				} else
					break;
			}
			// Handle to server and wait
			// Release Semaphore
			sem_signal(server_sem);
			sem_wait(client_sem);	
		} while ((segptr->moredata)&&(strcmp(segptr->result,"OVER")!=0));
	}
	closehandshake();
	return result;
}
linkedlist* clientrandomtag()
{
	inithandshake();
	// Wait for Semaphore
	#ifndef NOIPCNOTIFICATION
	printf("Waiting for semaphore(Server to become free)!!!\n");
	#endif
       
       	sem_wait(client_sem);

	// Got Semaphore 
	#ifndef NOIPCNOTIFICATION
	printf("Server is free, sending data!!!\n");
	#endif
       	
       	strcpy(segptr->command,"randomtag");
		
	#ifndef NOIPCNOTIFICATION
	printf("Sending command randomtag!!!\n");
	#endif
       	
	// Release Semaphore
	sem_signal(server_sem);
	sem_wait(client_sem);
	
	linkedlist* result=new linkedlist();
	int i;	
	if (strcmp(segptr->result,"OVER")!=0) {
		// We have some data
		do {
			for (i=0;i<DTP;i++) {
				if (strlen(segptr->data[i])!=0) {
					result->add(segptr->data[i]);
			//		printf("%s ",segptr->data[i]);
				} else
					break;
			}
			// Handle to server and wait
			// Release Semaphore
			sem_signal(server_sem);
			sem_wait(client_sem);	
		} while ((segptr->moredata)&&(strcmp(segptr->result,"OVER")!=0));
	}
	closehandshake();
	return result;
}

linkedlist* clientlistalltags()
{
	inithandshake();
	// Wait for Semaphore
	#ifndef NOIPCNOTIFICATION
	printf("Waiting for semaphore(Server to become free)!!!\n");
	#endif
       
       	sem_wait(client_sem);

	// Got Semaphore 
	#ifndef NOIPCNOTIFICATION
	printf("Server is free, sending data!!!\n");
	#endif
       	
       	strcpy(segptr->command,"listalltags");
		
	#ifndef NOIPCNOTIFICATION
	printf("Sending command listallfiles!!!\n");
	#endif
       	
	// Release Semaphore
	sem_signal(server_sem);
	sem_wait(client_sem);
	
	linkedlist* result=new linkedlist();
	int i;	
	if (strcmp(segptr->result,"OVER")!=0) {
		// We have some data
		do {
			for (i=0;i<DTP;i++) {
				if (strlen(segptr->data[i])!=0) {
					result->add(segptr->data[i]);
			//		printf("%s ",segptr->data[i]);
				} else
					break;
			}
			// Handle to server and wait
			// Release Semaphore
			sem_signal(server_sem);
			sem_wait(client_sem);	
		} while ((segptr->moredata)&&(strcmp(segptr->result,"OVER")!=0));
	}
	closehandshake();
	return result;
}

linkedlist* clientgetsuggestions() {
	linkedlist* result=new linkedlist();
	int i;	
	inithandshake();
	// Get Suggestions

	// Wait for Semaphore
	#ifndef NOIPCNOTIFICATION
	printf("Waiting for semaphore(Server to become free)!!!\n");
	#endif
       
       	sem_wait(client_sem);

	// Got Semaphore 
	#ifndef NOIPCNOTIFICATION
	printf("Server is free, sending data!!!\n");
	#endif
       	
       	strcpy(segptr->command,"getsuggestions");
		
	#ifndef NOIPCNOTIFICATION
	printf("Sending command getsuggestions!!!\n");
	#endif
       	
	// Release Semaphore
	sem_signal(server_sem);
	sem_wait(client_sem);
	
	if (strcmp(segptr->result,"OVER")!=0) {
		// We have some data
		do {
			for (i=0;i<DTP;i++) {
				if (strlen(segptr->data[i])!=0) {
					result->add(segptr->data[i]);
					//printf("%s ",segptr->data[i]);
				} else
					break;
			}
			// Handle to server and wait
			// Release Semaphore
			sem_signal(server_sem);
			sem_wait(client_sem);	
		} while ((segptr->moredata)&&(strcmp(segptr->result,"OVER")!=0));
	}
	closehandshake();
	return result;
}

linkedlist* clientsearch(char *which, linkedlist* tags) {
	char *tagsString;
	inithandshake();
	// Get files 

	// Wait for Semaphore
	#ifndef NOIPCNOTIFICATION
	printf("Waiting for semaphore(Server to become free)!!!\n");
	#endif
       
       	sem_wait(client_sem);

	// Got Semaphore 
	#ifndef NOIPCNOTIFICATION
	printf("Server is free, sending data!!!\n");
	#endif
       	
	tagsString = tags->toCSVString();
       	strcpy(segptr->command,which);
       	strcpy(segptr->param1,tagsString);
	printf("para for SEARCH %s",segptr->param1);
	free(tagsString);
		
	#ifndef NOIPCNOTIFICATION
	printf("Sending command %s!!!\n",which);
	#endif
       	
	// Release Semaphore
	sem_signal(server_sem);
	sem_wait(client_sem);
	
	linkedlist* result=new linkedlist();
	int i;	
	if (strcmp(segptr->result,"OVER")!=0) {
		// We have some data
		do {
			for (i=0;i<DTP;i++) {
				if (strlen(segptr->data[i])!=0) {
					result->add(segptr->data[i]);
					//printf("%s ",segptr->data[i]);
				} else
					break;
			}
			// Handle to server and wait
			// Release Semaphore
			sem_signal(server_sem);
			sem_wait(client_sem);	
		} while ((segptr->moredata)&&(strcmp(segptr->result,"OVER")!=0));
	}
	//isitreq
	closehandshake();
#ifdef VERIFYPERMISSION	
	linkedlist *tmp=new linkedlist();
	while ((result!=NULL) && (!result->isEmpty())) {
			char str[300];
			strcpy(str,result->remove());
			if (SOMEACCESS(str))
				tmp->add(str);
	}
	delete result;
	return tmp;
#else
	return result;
#endif
}

void clientcopyfile(char *src,char *dest){
	inithandshake();

	// Wait for Semaphore
	#ifndef NOIPCNOTIFICATION
	printf("Waiting for semaphore(Server to become free)!!!\n");
	#endif
       
       	sem_wait(client_sem);

	// Got Semaphore 
	#ifndef NOIPCNOTIFICATION
	printf("Server is free, sending data!!!\n");
	#endif
	#ifndef NOIPCNOTIFICATION
	printf("Sending command copyfile!!!\n");
	#endif

	strcpy(segptr->command,"copyfile");
	strcpy(segptr->param1,src);
	strcpy(segptr->param2,dest);

	sem_signal(server_sem);
	sem_wait(client_sem);	
	closehandshake();
}

void clientmovefile(char *src,char *dest){
	inithandshake();

	// Wait for Semaphore
	#ifndef NOIPCNOTIFICATION
	printf("Waiting for semaphore(Server to become free)!!!\n");
	#endif
       
       	sem_wait(client_sem);

	// Got Semaphore 
	#ifndef NOIPCNOTIFICATION
	printf("Server is free, sending data!!!\n");
	#endif
	#ifndef NOIPCNOTIFICATION
	printf("Sending command movefile!!!\n");
	#endif

	strcpy(segptr->command,"movefile");
	strcpy(segptr->param1,src);
	strcpy(segptr->param2,dest);

	sem_signal(server_sem);
	sem_wait(client_sem);	
	closehandshake();
}

void clientremovefile(char *src){
	inithandshake();

	// Wait for Semaphore
	#ifndef NOIPCNOTIFICATION
	printf("Waiting for semaphore(Server to become free)!!!\n");
	#endif
       
       	sem_wait(client_sem);

	// Got Semaphore 
	#ifndef NOIPCNOTIFICATION
	printf("Server is free, sending data!!!\n");
	#endif
	#ifndef NOIPCNOTIFICATION
	printf("Sending command removefile!!!\n");
	#endif

	strcpy(segptr->command,"removefile");
	strcpy(segptr->param1,src);
	
	sem_signal(server_sem);
	sem_wait(client_sem);	
	closehandshake();
}

void clientendserver() {
	inithandshake();

	// Wait for Semaphore
	#ifndef NOIPCNOTIFICATION
	printf("Waiting for semaphore(Server to become free)!!!\n");
	#endif
       
       	sem_wait(client_sem);

	// Got Semaphore 
	#ifndef NOIPCNOTIFICATION
	printf("Server is free, sending data!!!\n");
	#endif
	#ifndef NOIPCNOTIFICATION
	printf("Sending command removefile!!!\n");
	#endif

	strcpy(segptr->command,"endserver");
	
	sem_signal(server_sem);
	sem_wait(client_sem);	
	closehandshake();
}

void clientremovefilerecurse(char *src){
	inithandshake();

	// Wait for Semaphore
	#ifndef NOIPCNOTIFICATION
	printf("Waiting for semaphore(Server to become free)!!!\n");
	#endif
       
       	sem_wait(client_sem);

	// Got Semaphore 
	#ifndef NOIPCNOTIFICATION
	printf("Server is free, sending data!!!\n");
	#endif
	#ifndef NOIPCNOTIFICATION
	printf("Sending command removefilerecurse!!!\n");
	#endif

	strcpy(segptr->command,"removefilerecurse");
	strcpy(segptr->param1,src);
	
	sem_signal(server_sem);
	sem_wait(client_sem);	
	closehandshake();
}

long clientfilesinindex(){
	char data[50];
	inithandshake();

	// Wait for Semaphore
	#ifndef NOIPCNOTIFICATION
	printf("Waiting for semaphore(Server to become free)!!!\n");
	#endif
       
       	sem_wait(client_sem);

	// Got Semaphore 
	#ifndef NOIPCNOTIFICATION
	printf("Server is free, sending data!!!\n");
	#endif
	#ifndef NOIPCNOTIFICATION
	printf("Sending command removefilerecurse!!!\n");
	#endif

	strcpy(segptr->command,"filetriesize");
	
	sem_signal(server_sem);
	sem_wait(client_sem);
	strcpy(data,segptr->param2);
	closehandshake();
	return atol(data);
}

long clienttagsinindex() {
	char data[50];
	inithandshake();

	// Wait for Semaphore
	#ifndef NOIPCNOTIFICATION
	printf("Waiting for semaphore(Server to become free)!!!\n");
	#endif
       
       	sem_wait(client_sem);

	// Got Semaphore 
	#ifndef NOIPCNOTIFICATION
	printf("Server is free, sending data!!!\n");
	#endif
	#ifndef NOIPCNOTIFICATION
	printf("Sending command removefilerecurse!!!\n");
	#endif

	strcpy(segptr->command,"tagtriesize");
	
	sem_signal(server_sem);
	sem_wait(client_sem);
	strcpy(data,segptr->param2);
	closehandshake();
	return atol(data);
}
 
void autofoldertag(char *filename)
{
	char folder[512];
	int length= strlen(filename)-strlen(strrchr(filename,'/'));
	
	strncpy(folder, filename, length);	
	folder[length]='\0';	
	printf("Filename is %s\n", strrchr(filename,'/'));
	printf("Folder is %s\n", folder);
	
	foldertag(folder, strrchr(filename, '/'));
	
}

void foldertag(char *folder, char *filename)
{
    	linkedlist *autotags = new linkedlist();
	
#ifdef NOIPCNOTIFICATION
	printf("Autotag: %s %s\n", filename, folder);
#endif
	
	autotags->add(folder);
	clientaddtags(filename, autotags);
	
	autotags->removeAll();
	delete autotags;
}

#define FALSE 0
#define TRUE !FALSE
 
extern  int alphasort();
 


int files_select(const struct direct *entry)
{
	if ((strcmp(entry->d_name, ".") == 0) ||
		(strcmp(entry->d_name, "..") == 0))
		return (FALSE);
	else
		return (TRUE);
}

char* get_absolute_path(char *pathname)
{
	/* TODO: try to expand the pathname fully here */

	return pathname;
}
 
void autotag( char* pathname)
{
	int count,i;
	struct direct **files;
	int file_select();
        char fullpathname[MAXPATHLEN];
	linkedlist *filelst;
	struct stat PathStat;
	char *cmdtok,*cmdin;  
        bool nullcnt = 0;	
	//YV 6-6-08: 
	filelst = new linkedlist();
	//filelst->add(pathname);
/*	
	cmdin = pathname;
	cmdtok=strtok(cmdin," ");
	
	while(1)
	{
		printf("\nToken is %s",cmdtok);
		pathname = cmdtok;
		cmdtok=strtok(NULL," ");
		if(cmdtok == NULL)
		{
			if(nullcnt == 0)
			{
				nullcnt = 1;
				continue;
			}
			else
				break;
		}

	}
	printf("\nToken is %s",cmdtok);	
	return;*/
	//pathname=getabsmod(pathname);
	printf("path converted to %s\n",pathname);
	filelst->add(pathname);
	while(!filelst->isEmpty())
	{
		pathname = filelst->remove();
		printf("the pathanme removed from the list%s\n",pathname);
	//added by YV 6-6-08. This is to handle the relative paths
	if(pathname[0]!='/')
	{
		sprintf(fullpathname, "%s/./%s", get_current_dir_name(),pathname);
		strcpy(pathname,fullpathname);
	}
	else if(pathname[0]=='.')
	{
		if(pathname[1]!='/')
		{
			pathname+=2;
			sprintf(fullpathname, "%s/%s", get_current_dir_name(),pathname);
			//pathname=getabsmod(fullpathname);
		}
		//else
		//{
		//	printf("Invalid path provided. No tags are added. Please try again.");
		//	return;
		//}

	}
	else
		strcpy(fullpathname,pathname);
	printf("fullpath %s\n",fullpathname);
	pathname=getabsmod(fullpathname);
	printf("path returned %s",pathname);
	if(pathname[strlen(pathname)-1]=='/' || pathname[strlen(pathname)-1]=='\\')
		pathname[strlen(pathname)-1]='\0'; 

	printf("Tagging files in path = %s\n",pathname);
	strcpy(fullpathname,pathname);
	count = scandir(fullpathname, &files, files_select, alphasort);
	 
	/* If no files found, make a non-selectable menu item */
	if(count <= 0)
	{ 
		printf("No files in this directory\n");
		//return;
		continue;
	}

	printf("Number of files = %d\n",count);
	for (i=1;i<=count;++i)
	{
		sprintf(fullpathname, "%s/%s", pathname, files[i-1]->d_name);
		stat(fullpathname,&PathStat);
		if(S_ISLNK(PathStat.st_mode))
			lstat(fullpathname,&PathStat);
		if(S_ISDIR(PathStat.st_mode))
		{
			printf("%s is a directory.\n", fullpathname);
			printf("Adding %s to list.\n",fullpathname);
			filelst->add(fullpathname);
		}
		/*! TODO: recurse through folder names here */
		foldertag(get_absolute_path(pathname), fullpathname);
	}
	}
	printf("\n");
}
